package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class TrackInfoServiceImpl extends ServiceImpl<TrackInfoMapper, TrackInfo> implements TrackInfoService {

	@Autowired
	private TrackInfoMapper trackInfoMapper;

	@Autowired
	private AlbumInfoMapper albumInfoMapper;

	@Autowired
	private TrackStatMapper trackStatMapper;

	@Autowired
	private VodService vodService;


	@Override
	public void updateTrackInfo(Long trackId, TrackInfoVo trackInfoVo) {
		//	 修改声音 track_info
		//	考虑是否修改了声音： 每个流媒体Id 唯一！
		//	trackInfoVo 中有 修改之后的 流媒体Id 与 之前声音表中的流媒体Id 进行比较！
		TrackInfo trackInfo = null;
		try {
			trackInfo = this.getById(trackId);
			//	原始流媒体Id
			String mediaFileId = trackInfo.getMediaFileId();
			//	如果没有修改过声音： 属性拷贝
			BeanUtils.copyProperties(trackInfoVo, trackInfo);
			//	判断原始流媒体Id 123 与 当前流媒体Id 234 是否相同
			if (!mediaFileId.equals(trackInfoVo.getMediaFileId())) {
				//	修改了 声音流媒体Id
				//	删除云点播中的数据
				vodService.removeByMediaFileId(mediaFileId);
				//	根据最新的流媒体Id 获取到声音的时长.
				TrackMediaInfoVo trackMediaInfoVo = vodService.getMediaInfo(trackInfoVo.getMediaFileId());
				trackInfo.setMediaType(trackMediaInfoVo.getType());
				trackInfo.setMediaDuration(trackMediaInfoVo.getDuration());
				trackInfo.setMediaUrl(trackMediaInfoVo.getMediaUrl());
				trackInfo.setMediaSize(trackMediaInfoVo.getSize());
				//	修改数据 因为流媒体数据要改变.
				//  this.updateById(trackInfo);
			}
		} catch (BeansException e) {
			log.error("当前 trackId {}", trackId);
			throw new RuntimeException(e);
		}
		//	原始的声音统计数据是否要删除！
		//	更新数据
		this.updateById(trackInfo);

	}

	@Override
	public TrackInfo getTrackInfoById(Long trackId) {
		//	查询声音表；
		return this.getById(trackId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void removeTrackInfoById(Long trackId) {
		//	通过声音Id 获取到 声音对象
		TrackInfo trackInfo = this.getById(trackId);
		//	track_info; is_deleted=1;
		this.removeById(trackId);
		//	track_stat; delete from track_stat where track_id = ? and is_deleted=0; 物理删除
		//	使用逻辑删除  update track_stat set is_deleted=1 where track_id = ? and is_deleted=0;
		trackStatMapper.delete(new LambdaQueryWrapper<TrackStat>().eq(TrackStat::getTrackId,trackId));
		//	先查询到当前专辑对象
		AlbumInfo albumInfo = this.albumInfoMapper.selectById(trackInfo.getAlbumId());
		albumInfo.setIncludeTrackCount(albumInfo.getIncludeTrackCount() - 1);
		//	修改专辑包含的声音数据
		//	update album_info set include_track_count = include_track_count -1 where id = ? and is_deleted=0;
		this.albumInfoMapper.updateById(albumInfo);
		//  修改排序编号值.
		//	update track_info set order_num = order_num - 1 where order_num > 50 and album_id = 2 and is_deleted = 0;
		trackInfoMapper.updateOrderNum(trackInfo.getOrderNum(), albumInfo.getId());
		//	删除云点播
		vodService.removeByMediaFileId(trackInfo.getMediaFileId());

	}

	@Override
	public IPage<TrackListVo> findUserTrackPage(Page<TrackListVo> trackListVoPage, TrackInfoQuery trackInfoQuery) {
		//	查询数据
		return trackInfoMapper.selectUserTrackPage(trackListVoPage, trackInfoQuery);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveTrackInfo(TrackInfoVo trackInfoVo, Long userId) {
		//	track_info
		TrackInfo trackInfo = new TrackInfo();
		//	属性拷贝：
		BeanUtils.copyProperties(trackInfoVo, trackInfo);
		//	user_id
		trackInfo.setUserId(userId);
		//  order_num 第一种方式：查询声音表中同一张专辑的最大order_num; 第二种：专辑包含声音总数include_track_count + 1;
		int orderNum = 0;
		//	查询专辑表中声音总数； 1. 查询专辑对象
		AlbumInfo albumInfo = albumInfoMapper.selectById(trackInfoVo.getAlbumId());
		if (null != albumInfo) {
			orderNum = albumInfo.getIncludeTrackCount() + 1;
		}
		trackInfo.setOrderNum(orderNum);
		//	media_duration media_size media_type 等数据为空！ 调用云点播的api获取流媒体数据！
		TrackMediaInfoVo trackMediaInfoVo = vodService.getMediaInfo(trackInfoVo.getMediaFileId());
		//	赋值流媒体数据
		trackInfo.setMediaDuration(trackMediaInfoVo.getDuration());
		trackInfo.setMediaType(trackMediaInfoVo.getType());
		trackInfo.setMediaUrl(trackMediaInfoVo.getMediaUrl());
		trackInfo.setMediaSize(trackMediaInfoVo.getSize());
		trackInfoMapper.insert(trackInfo);
		//	track_stat - 统计信息：
		this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_PLAY);
		this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_COLLECT);
		this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_PRAISE);
		this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_COMMENT);
		// 	album_info
		albumInfo.setIncludeTrackCount(albumInfo.getIncludeTrackCount() + 1);
		this.albumInfoMapper.updateById(albumInfo);

	}

	@Transactional(rollbackFor = Exception.class)
	public void saveTrackStat(Long trackId, String statType) {
		//  创建对象
		TrackStat trackStat = new TrackStat();
		trackStat.setTrackId(trackId);
		trackStat.setStatType(statType);
		trackStat.setStatNum(new Random().nextInt(100));
		trackStatMapper.insert(trackStat);
	}

}
